/*
 * Copyright (c) 2009, EzWare
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.Redistributions 
 * in binary form must reproduce the above copyright notice, this list of 
 * conditions and the following disclaimer in the documentation and/or 
 * other materials provided with the distribution.Neither the name of the 
 * EzWare nor the names of its contributors may be used to endorse or 
 * promote products derived from this software without specific prior 
 * written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 */

package org.chaser.mvc

import javax.swing._
import scala.swing._

import java.awt.KeyboardFocusManager
import java.awt.SystemColor
import java.awt.Font
import net.miginfocom.swing.MigLayout

protected case class ControllerDialog( controller: ViewController[_], private val resizable: Boolean ) {

    lazy val header = new DialogHeader();
    lazy val headerSeparator = new JSeparator();
  	private var result = false

  
	private val actionOK =  scala.swing.Action("OK"){ 
	    result = true
        dlg.setVisible(false);
	}
 
	private val actionCancel = scala.swing.Action("Cancel"){
		dlg.setVisible(false);                                 
    }
 
    private val okButton = new Button( actionOK ).peer
  
  
	private val dlg = new JDialog( 
	  KeyboardFocusManager.getCurrentKeyboardFocusManager.getFocusedWindow(), 
      controller.title )
 
    dlg.setModal( true );
    dlg.setResizable( resizable );
    dlg.setIconImage( null ) //controller.icon;
    dlg.setContentPane( buildContentPanel().peer)
    setHeaderVisible(true)
    
    
    private def buildContentPanel(): Panel = {
      
      new Panel {
      
	      peer.setLayout( new MigLayout("fill"))
	
	      header.setTitle( controller.title )
          header.setInstruction( controller.instruction )
          header.setIcon( controller.icon );
       
		  peer.add( header.peer, "north, hidemode 2" );
		  peer.add( headerSeparator, "north, hidemode 2");
	   
          val scroller = new JScrollPane(controller.view.peer)
          scroller.setBorder(null);
		  peer.add(scroller, "dock center");
	   
	      val buttonPanel = new JPanel( new MigLayout("nogrid, fillx, aligny 100%, gapy unrel") )

	      dlg.getRootPane.setDefaultButton( null );
       
		  buttonPanel.add( okButton, "tag ok");
		  buttonPanel.add(new Button( actionCancel ).peer, "tag cancel");
	      
	      peer.add( buttonPanel, "south");
		  peer.add(new JSeparator(), "south");
	      
      }
      
    } 

    //TODO: make it a property
    private def setHeaderVisible( visible: Boolean ) {
       header.peer.setVisible(visible)
       headerSeparator.setVisible(visible)
    }
    
    def execute: Boolean = {
       dlg.pack
       dlg.setLocationRelativeTo(null)
       dlg.getRootPane.setDefaultButton( okButton )
       dlg.setVisible( true )
       result
    }

}

//TODO: Make sure pref width of header does not exeed pref width of content 

protected case class DialogHeader() extends Panel {

  private lazy val lbTitle = new JLabel()
  private lazy val lbInstruction = new JLabel()
  private lazy val lbIcon = new JLabel()
  peer.setLayout( new MigLayout("fill, insets panel"))
  
  peer.setBorder( BorderFactory.createEmptyBorder(10,10,10,10))
  peer.setBackground( SystemColor.window )
  
  lbIcon.setVerticalAlignment(SwingConstants.TOP)
  
  val originalFont  = lbTitle.getFont();
  lbTitle.setFont( originalFont.deriveFont( Font.BOLD, originalFont.getSize()+1 ) )
  
  peer.add( lbTitle, "dock north, hidemode 2")
  peer.add( lbIcon,  "dock east,  hidemode 2")
  peer.add( lbInstruction, "dock center")

  
  def setTitle( title: String ) {
    lbTitle.setText(title)
    lbTitle.setVisible( title != null && title.trim.length > 0 )
  }
  
  def setInstruction( instruction: String ) { lbInstruction.setText(instruction) }
  
  def setIcon( icon: Icon ) { lbIcon.setIcon(icon) }
  
}
